﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VISS.API.Users;
using System.IO;
using NCT.Framework;

namespace VISS.API.Users.Update {
    public class UserUpdate {

        public static void UpdateAvatarAndImagePath(string userImagePath) {
            DateTime changeDate = new DateTime(2010, 10, 15, 17, 40, 0);
            string queryString = "select * from [user] (nolock) where datecreated <='" + changeDate.ToString("MM-dd-yyyy hh:mm") + "'";
            string connectionName = ConfigInfo.ConnectionName;
            List<UserEnt> userList = UserDA.GetByQueryString(queryString, connectionName, null, null);
            UserAvatarEnt avatar = null;
            StreamWriter logFile = new StreamWriter("log.txt", true, Encoding.Unicode);
            logFile.Write("------------------------" + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " Begin Change Avatar & Image Path--------------------------------------\r\n");
            List<UserImageEnt> imageList = null;
            string oldAvatarPath;
            string oldAvatarThumbPath;
            string newAvatarFolderPath;
            string newAvatarThumbPath;
            for (int i = 0; i < userList.Count; i++) {
                // Update Avatar
                avatar = UserAvatarDA.GetByUserKey(userList[i].UserKey, connectionName, null, null);
                if (avatar != null && avatar.DateModified.CompareTo(changeDate) < 0) {
                    oldAvatarPath = userImagePath + avatar.FileName;
                    oldAvatarThumbPath = oldAvatarPath.Replace(".jpg", "-thumb.jpg");
                    newAvatarFolderPath = "avatar/" + userList[i].DateCreated.Year + "/" + userList[i].DateCreated.Month + "/" + userList[i].DateCreated.Day + "/";
                    newAvatarThumbPath = (newAvatarFolderPath + avatar.FileName).Replace(".jpg", "-thumb.jpg");
                    if (FileUtils.CreateFolderForFile(userImagePath + newAvatarFolderPath)) {
                        if(FileUtils.Copy(oldAvatarPath, userImagePath + newAvatarFolderPath + avatar.FileName)) {
                            if(FileUtils.Copy(oldAvatarThumbPath, userImagePath + newAvatarThumbPath)) {
                                avatar.FileName = newAvatarFolderPath;
                                if(UserAvatarDA.Update(avatar, connectionName, null, null) <= 0) {
                                    logFile.Write("Fail Update DB: " + avatar.UserKey + "\r\n");
                                }
                            }
                            else {
                                logFile.Write("Fail Copy Thumb File: " + avatar.UserKey + "\r\n");
                            }
                        }
                        else {
                            logFile.Write("Fail Copy File: " + avatar.UserKey + "\r\n");
                        }
                    }
                    else {
                        logFile.Write("Fail Create Folder: " + avatar.UserKey + "\r\n");
                    }
                }

                // Update Image
                //imageList = UserImageDA.GetByUserKey(userList[i].UserKey, connectionName, null, null);
                //if (imageList.Count > 0) {
                //}
            }
            logFile.Write("----------------------------End Change Avatar & Image Path---------------------------\r\n");
            logFile.Close();
        }
    }
}
